package com.furentech.iotserver.fa.sinopec.sinopec_processor;


import com.furentech.iotserver.fa.sinopec.SinopecFunCode;
import com.furentech.iotserver.fa.utils.FileUtils;
import com.furentech.iotserver.fa.utils.SpringUtil;
import io.netty.channel.Channel;
import org.springframework.stereotype.Component;

@Component
public class ReqData_Processor extends SinopecProcessorBase {
    @Override
    String actualGetJsonString(byte[] buf) {
        return null;
    }

    @Override
    String actualGetReadableString(byte[] buf) {
        return String.format("加油机请求下载数据，文件代码:0x%02x\r\n", buf[1]);
    }

    @Override
    byte[] actualGetResponse(byte[] buf, Channel channel) {

        byte[] resByteBuf;

        resByteBuf = new byte[6];

        long length = getContentLength(buf[1]);

        resByteBuf[0] = SinopecFunCode.Data_Bytes;
        resByteBuf[1] = (byte) ((length / 65535 / 256) & 0x0000000000ff);
        resByteBuf[2] = (byte) ((length / 65535 ) & 0x0000000000ff);
        resByteBuf[3] = (byte) ((length / 256) & 0x0000000000ff);
        resByteBuf[4] = (byte) ((length ) & 0x0000000000ff);
        resByteBuf[5] = buf[1];

        return  resByteBuf;
    }


    long getContentLength(byte funCode) {
        String filePathPrefix=System.getProperty("user.dir")+System.getProperty("file.separator");

        String fileName = filePathPrefix+String.format("%d.dat", funCode);

        long fileSize=0;

        fileSize = FileUtils.getFileSizeInBytes(fileName);

        return fileSize;
    }

}


